package com.xoa.service.document.Impl;

import com.xoa.dao.common.SysCodeMapper;
import com.xoa.dao.department.DepartmentMapper;
import com.xoa.dao.document.DocumentExchangeSetMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.model.common.SysCode;
import com.xoa.model.document.DocumentExchangeSet;
import com.xoa.model.users.Users;
import com.xoa.service.document.DocumentExchangeSetService;
import com.xoa.util.CookiesUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.page.PageParams;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

import static oracle.net.aso.C00.s;

@Service
public class DocumentExchangeSetServiceImpl implements DocumentExchangeSetService {
    @Resource
    private DocumentExchangeSetMapper documentExchangeSetMapper;
    @Resource
    private DepartmentMapper departmentMapper;
    @Resource
    private UsersMapper usersMapper;
    @Resource
    private SysCodeMapper sysCodeMapper;


    //查询所有数据
    public ToJson<DocumentExchangeSet> selectDocumentExchange(DocumentExchangeSet documentExchangeSet, PageParams pageParams) {
        ToJson<DocumentExchangeSet> tojson = new ToJson<DocumentExchangeSet>();
        Map<String, Object> map = new HashMap<>();
        map.put("page", pageParams);
        map.put("obj", documentExchangeSet);
        List<DocumentExchangeSet> documentExchangeSets = documentExchangeSetMapper.selectByExample(map);
        for (DocumentExchangeSet documentExchangeSet1 : documentExchangeSets
        ) {
            String exchangeUserId = "";
            String[] ExchangeUserIds = documentExchangeSet1.getExchangeUserId().split(",");
            for (int i = 0; i < ExchangeUserIds.length; i++) {
                exchangeUserId = exchangeUserId + usersMapper.getUsernameByUserId(ExchangeUserIds[i]) + ",";
            }
            String ChildDepts = "";
            if (documentExchangeSet1.getChildDepts() != null && documentExchangeSet1.getChildDepts().length() > 0) {
                String[] ChildDept = documentExchangeSet1.getChildDepts().split(",");
                for (int i = 0; i < ChildDept.length; i++) {
                    ChildDepts = ChildDepts + departmentMapper.getDeptNameById(Integer.parseInt(ChildDept[i])) + ",";
                }
                documentExchangeSet1.setChildDeptsName(ChildDepts);
            }
            if (documentExchangeSet1.getParentDept() != null && documentExchangeSet1.getParentDept() != "") {
                documentExchangeSet1.setParentDeptName(departmentMapper.getDeptNameById(Integer.parseInt(documentExchangeSet1.getParentDept())));
            }
            documentExchangeSet1.setExchangeDeptName(departmentMapper.getDeptNameById(documentExchangeSet1.getExchangeDeptId()));
            documentExchangeSet1.setExchangeUserName(exchangeUserId);
            documentExchangeSet1.setCreateUserName(usersMapper.getUsernameByUserId(documentExchangeSet1.getCreateUserId()));
            documentExchangeSet1.setUnitTypeName(sysCodeMapper.getSingleCode("DOC_EXCHANGE_UNIT_TYPE", documentExchangeSet1.getUnitType()).getCodeName());
        }
        tojson.setFlag(0);
        tojson.setObj(documentExchangeSets);
        tojson.setTotleNum(pageParams.getTotal());
        return tojson;
    }

    //根据权限查询数据
    public ToJson<DocumentExchangeSet> selectByDocumentPriv(DocumentExchangeSet documentExchangeSet) {
        ToJson<DocumentExchangeSet> tojson = new ToJson<DocumentExchangeSet>();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Cookie redisSessionId = CookiesUtil.getCookieByName(request, "redisSessionId");
        Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionId);
        Map<String, Object> map = new HashMap<>();
        documentExchangeSet.setDocumentPriv("1");
        map.put("obj", documentExchangeSet);
        Map<String, List<DocumentExchangeSet>> maps = new HashMap();
        List<DocumentExchangeSet> documentExchangeSets = documentExchangeSetMapper.selectByExample(map);
        List<DocumentExchangeSet> documentExchangeSets1 = documentExchangeSetMapper.selectAllByPriv();
        Boolean res = true;
        //取出所有符合的depts
        String depts = "";//符合的所有部门
        for (DocumentExchangeSet des : documentExchangeSets1) {
            depts = depts + des.getExchangeDeptId() + ",";
        }
        //判断公文设置的当前登陆人是否含有子部门
        for (DocumentExchangeSet des : documentExchangeSets1) {
            String[] exchangeUserId = des.getExchangeUserId().split(",");
            if (Arrays.asList(exchangeUserId).contains(user.getUserId()) && des.getChildDepts() != null && des.getChildDepts() != "") {//判断是否由子部门
                String[] childDepts = des.getChildDepts().split(",");
                for (int i = 0; i < childDepts.length; i++) {
                    if (Arrays.asList(depts.split(",")).contains(childDepts[i])) {
                        DocumentExchangeSet dpo = new DocumentExchangeSet();
                        dpo.setExchangeDeptId(Integer.parseInt(childDepts[i]));
                        maps.put("obj", documentExchangeSetMapper.selectByExchangeDeptId(dpo));
                    }
                }
                res = false;//若有子部门，则不筛选其他部门
                break;
            }
        }
        if (res) {
            maps.put("obj", documentExchangeSets);
        }
        tojson.setFlag(0);
        tojson.setObj(documentExchangeSetMapper.selectByUser(user.getUserId()));
        tojson.setObject(maps);
        return tojson;
    }

    //判断是否有权限发文
    public ToJson selectByUser(HttpServletRequest request) {
        ToJson tojson = new ToJson<>();
        int flag = 0;
        Cookie redisSessionId = CookiesUtil.getCookieByName(request, "redisSessionId");
        Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionId);
        List<DocumentExchangeSet> documentExchangeSets = documentExchangeSetMapper.selectByUser(user.getUserId());
        if (documentExchangeSets.size() > 0) {
            flag = 1;
        }
       /* for (DocumentExchangeSet documentExchangeSet1 : documentExchangeSets
        ) {
            String[] exchangeUserId = documentExchangeSet1.getExchangeUserId().split(",");
            if (Arrays.asList(exchangeUserId).contains(user.getUserId())) {
                String[] documentPriv = documentExchangeSet1.getDocumentPriv().split(",");
                if (Arrays.asList(documentPriv).contains("2")) {
                    flag = 1;
                }
            }
        }*/
        tojson.setObject(flag);
        tojson.setFlag(0);
        return tojson;
    }

    public int saveDocumentExchange(HttpServletRequest request, DocumentExchangeSet documentExchangeSet) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        Users sessionInfo = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionCookie);
        documentExchangeSet.setCreateUserId(sessionInfo.getUserId());
        documentExchangeSet.setCreateTime(new Date());
        return documentExchangeSetMapper.insert(documentExchangeSet);
    }

    public int deleteDocumentExchange(Integer exchangeId) {
        return documentExchangeSetMapper.deleteByPrimaryKey(exchangeId);
    }

    public int updateDocumentExchange(DocumentExchangeSet documentExchangeSet) {
        return documentExchangeSetMapper.updateByPrimaryKeySelective(documentExchangeSet);
    }

    public DocumentExchangeSet selectDocumentExchangeById(Integer exchangeId) {
        return documentExchangeSetMapper.selectByPrimaryKey(exchangeId);
    }

}
